package com.example.gzysmartcity.ui.fragment.sell;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentCustomBinding;
import com.example.gzysmartcity.repository.model.PersonExpend;
import com.example.gzysmartcity.ui.adapter.CustomRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.CustomViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CustomFragment extends BaseFragment<FragmentCustomBinding> {
    @Override
    protected FragmentCustomBinding viewBinding() {
        return FragmentCustomBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    private boolean search = false;

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        CustomViewModel customViewModel = new ViewModelProvider(this).get(CustomViewModel.class);
        customViewModel.getPersonData();
        customViewModel.getCustomLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
                    List<PersonExpend> expends = customViewModel.getPersonExpendList();
                    CustomRvAdapter adapter = new CustomRvAdapter(R.layout.rv_custom);
                    adapter.setNewInstance(expends);
                    b.rvCustom.setAdapter(adapter);
                    b.rvCustom.setLayoutManager(new LinearLayoutManager(getContext()));
                    b.searchCustom.setOnCloseListener(() -> {
                        if (search) {
                            customViewModel.search("");
                            adapter.notifyDataSetChanged();
                            search = false;
                        }
                        return false;
                    });
                    b.searchCustom.setSubmitButtonEnabled(true);
                    b.searchCustom.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            b.searchCustom.setIconified(true);
                            customViewModel.search(query);
                            adapter.notifyDataSetChanged();
                            search = true;
                            return true;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            return false;
                        }
                    });
                    break;
                case FAIL:
                    break;
            }
        });


    }

    @Override
    protected String setTitle() {
        return "顾客列表";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }
}
